package com.real8.plusplayground.client.statistics;

import com.google.api.gwt.services.plus.shared.Plus;
import com.google.api.gwt.services.plus.shared.Plus.ActivitiesContext.ListRequest.Collection;
import com.google.api.gwt.services.plus.shared.model.ActivityFeed;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.real8.plusplayground.client.ClientFactory;

public class StatisticsActivity extends AbstractActivity implements
		StatisticsView.Presenter {
	private final ClientFactory clientFactory;
	private StatisticsView view;

	private final StatisticsPlace place;

	public StatisticsActivity(ClientFactory clientFactory, StatisticsPlace place) {
		this.clientFactory = clientFactory;
		this.place = place;
	}

	@Override
	public void start(final AcceptsOneWidget panel, EventBus eventBus) {
		// initialize view
		view = clientFactory.getStatisticsView();
		view.setPresenter(this);

		// set id
		final String personId = place.getPersonId();
		view.setId(personId);

		// check for "me"
		if (personId.equalsIgnoreCase("me")) {
			// TODO investigate checking first if already logged in
			clientFactory.login(new Receiver<String>() {
				@Override
				public void onSuccess(String authorizationToken) {
					clientFactory.createNewPlus(authorizationToken,
							new Receiver<Plus>() {
								@Override
								public void onSuccess(Plus response) {
									getAndSetStatistics(personId);
								}
							});
				}
			});

		} else {
			getAndSetStatistics(personId);
		}

		// set view as panel widget
		panel.setWidget(view.asWidget());
	}

	private void getAndSetStatistics(final String personId) {
		// check for plus
		Plus plus = clientFactory.getPlus();
		if (plus != null) {
			// get activities
			// TODO check for paging
			// TODO store results somewhere and only add new once
			plus.activities().list(personId, Collection.PUBLIC)
					.to(new Receiver<ActivityFeed>() {
						@Override
						public void onSuccess(ActivityFeed response) {
							// init stats
							PersonStatistics stats = new PersonStatistics();

							// process Activities
							for (com.google.api.gwt.services.plus.shared.model.Activity activity : response
									.getItems()) {
								// search mentions
								String content = activity.getPlusObject()
										.getContent();
								String[] splits1 = content.split("oid=\"");
								if (splits1.length > 1) {
									for (int iSplit1 = 1; iSplit1 < splits1.length; iSplit1++) {
										// get mentioned Id
										String mentionedIdAndMore = splits1[iSplit1];

										// remove everything after id
										String mentionedId = mentionedIdAndMore
												.substring(0,
														mentionedIdAndMore
																.indexOf("\""));

										// check not already listed
										if (!stats.getPersonsMentioned()
												.contains(mentionedId)) {
											// remember
											stats.getPersonsMentioned().add(
													mentionedId);
											// log
											println("Newly mentioned person found (in activity "
													+ activity.getId()
													+ "): "
													+ mentionedId);
										} else {
											// log
											println("Already listed mentioned person found (in activity "
													+ activity.getId()
													+ "): "
													+ mentionedId);
										}
									}
								}
							}

							// show statistics
							view.getEditorDriver().edit(stats);
						}
					}).fire();
		} else {
			println("Warning: Plus RequestFactory not (yet) initialized");
		}
	}

	@Override
	public void onIdChange(String personId) {
		clientFactory.getPlaceController().goTo(new StatisticsPlace(personId));
	}

	private void println(String value) {
		clientFactory.getShell().println(value);
	}
}
